package com.sfh.dog.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.widget.EditText;

import com.sfh.dog.DogApplication;
import com.sfh.dog.R;

public class FaceUtil
{
	public static final String FACE_ZHENGZE = "\\[([a-z|A-Z])\\]";

	/**
	 * 将文本中的表情符号替换为表情图片
	 * 
	 * @param text
	 *            需要转换的字符
	 * @return 带有表情的字符
	 */
	public static CharSequence replace(Context context, CharSequence text)
	{
		try
		{
			SpannableStringBuilder builder = new SpannableStringBuilder(text);
			Pattern patten = Pattern.compile(FACE_ZHENGZE,
					Pattern.CASE_INSENSITIVE);
			Matcher matcher = patten.matcher(text);
			while (matcher.find())
			{

				Bitmap bitmap = BitmapFactory.decodeResource(
						context.getResources(), R.drawable.good);
				int rawHeigh = bitmap.getHeight();
				int rawWidth = bitmap.getHeight();
				int newHeight = 30;
				int newWidth = 30;
				// 计算缩放因子
				float heightScale = ((float) newHeight) / rawHeigh;
				float widthScale = ((float) newWidth) / rawWidth;
				// 新建立矩阵
				Matrix matrix = new Matrix();
				matrix.postScale(heightScale, widthScale);
				Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, rawWidth,
						rawHeigh, matrix, true);
				ImageSpan span = new ImageSpan(context, newBitmap);
				builder.setSpan(span, matcher.start(), matcher.end(),
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			return builder;
		} catch (Exception e)
		{
			return text;
		}
	}

	public static SpannableString selectFace(Context context, int id)
	{
		Bitmap bitmap = BitmapFactory
				.decodeResource(context.getResources(), id);
		int rawHeigh = bitmap.getHeight();
		int rawWidth = bitmap.getHeight();
		int newHeight = 30;
		int newWidth = 30;
		// 计算缩放因子
		float heightScale = ((float) newHeight) / rawHeigh;
		float widthScale = ((float) newWidth) / rawWidth;
		// 新建立矩阵
		Matrix matrix = new Matrix();
		matrix.postScale(heightScale, widthScale);
		Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, rawWidth,
				rawHeigh, matrix, true);
		ImageSpan imageSpan = new ImageSpan(context, newBitmap);
		String emojiStr = DogApplication.mFace.get(id);
		SpannableString spannableString = new SpannableString(emojiStr);
		spannableString.setSpan(imageSpan, emojiStr.indexOf('['),
				emojiStr.indexOf(']') + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

		return spannableString;
	}

	/** 删除表情 */
	public static void deleFace(EditText msgEt)
	{
		int selection = msgEt.getSelectionStart();
		String text = msgEt.getText().toString();
		if (selection > 0)
		{
			String text2 = text.substring(selection - 1);
			if ("]".equals(text2))
			{
				int start = text.lastIndexOf("[");
				int end = selection;
				msgEt.getText().delete(start, end);
				return;
			}
			msgEt.getText().delete(selection - 1, selection);
		}
	}
}
